package com.resou.reader.mine.consumerecord;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.ConsumptionDataBean;
import com.resou.reader.api.entry.ConsumptionDetailBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.UserService;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.data.Injection;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.UserUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ConsumeRecordActivity extends BaseActivity {

    CompositeDisposable compositeDisposable = new CompositeDisposable();

    @BindView(R.id.swipe_refresh_layout)
    SmartRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.no_consume_view)
    RelativeLayout mNoConsumeView;
    @BindView(R.id.rlContent)
    RelativeLayout rlContent;

    private ConsumeAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private int page = 0;
    private boolean hasMore = true;
    private UserService mService;

    @Override
    public int getLayoutId() {
        return R.layout.activity_consum_record;
    }

    @Override
    protected void initPresenter() {
        mService = Injection.provideUserService();
    }

    @Override
    protected void initView() {
        mAdapter = new ConsumeAdapter(this);
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                getMoreData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mAdapter.clear();
                page = 0;
                hasMore = true;
                getMoreData();
            }
        });
        getMoreData();
    }

    @Override
    protected void retryRefreshData() {
        mAdapter.clear();
        page = 0;
        hasMore = true;
        getMoreData();
    }

    private void getMoreData() {
        if (!hasMore)
            return;
        String token = UserUtil.getToken();
        if (token.isEmpty()) {
            return;
        }
        mService.consumptionRecords(token, page, 50)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<ConsumptionDataBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(Result<ConsumptionDataBean> consumptionDataBeanResult) {
                        int code = consumptionDataBeanResult.getCode();
                        if (code == 0) {
                            ConsumptionDataBean dataBean = consumptionDataBeanResult.getData();
                            hasMore = dataBean.getPage() < dataBean.getPageTotal() - 1;
                            if (hasMore) {
                                mSwipeRefreshLayout.finishLoadMore();
                            } else {
                                mSwipeRefreshLayout.finishLoadMoreWithNoMoreData();
                            }
                            List<ConsumptionDetailBean> detailBeans = dataBean.getConsumptionRecord();
                            if ((detailBeans == null || detailBeans.size() == 0) && page == 0) {
                                mNoConsumeView.setVisibility(View.VISIBLE);
                                rlContent.setVisibility(View.GONE);
                                mSwipeRefreshLayout.finishRefresh();
                            } else {
                                rlContent.setVisibility(View.VISIBLE);
                                mNoConsumeView.setVisibility(View.GONE);
                                mAdapter.updateList(detailBeans, hasMore);
                            }
                            page++;
                        } else {
                            rlContent.setVisibility(View.VISIBLE);
                            mSwipeRefreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.makeShortToast("网络异常");
                        showNetErrorView(R.id.rlContent);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public Context getViewContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, ConsumeRecordActivity.class);
        context.startActivity(intent);
    }
}
